package com.kmx.demo.utils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

public class LoadProperties {
	
//	private String configPath = "conf/restclient.properties";
	private String configPath = "conf/config.properties";
	
	public LoadProperties(String path){
		this.configPath = path;
	}
	
	public LoadProperties(){
	}
	
	public Properties load(){
		try {
			FileReader reader = new FileReader(new File(this.configPath));
			Properties properties = new Properties();
			properties.load(reader);
			reader.close();
			return properties;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Map<String, String>  loadProperties(String filePath) {
		Map<String, String> map = new HashMap<String, String>();
		Properties properties = new LoadProperties(filePath).load();

		Set<Entry<Object, Object>> set = properties.entrySet();
		// 返回在此Set中的元素上进行迭代的迭代器
		Iterator<Map.Entry<Object, Object>> it = set.iterator();
		String key = null, value = null;
		// 循环取出key-value
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			key = String.valueOf(entry.getKey());
			value = String.valueOf(entry.getValue());
			key = key == null ? key : key.trim().toUpperCase();
			value = value == null ? value : value.trim().toUpperCase();
			// 将key-value放入map中
			map.put(key, value);
		}
		return map;
	}
}
